<?php

namespace App\Services\Sms\Drivers;

use App\Services\Sms\SmsSendResult;
use App\Services\Sms\SmsSender;
use Psr\Log\LoggerInterface;

class LogSmsSender implements SmsSender
{
    public function __construct(
        private readonly LoggerInterface $logger
    ) {
    }

    public function sendLoginCode(string $phone, string $code, int $ttlMinutes): SmsSendResult
    {
        return $this->logSend('login_code', $phone, [
            'code' => $code,
            'ttl_minutes' => $ttlMinutes,
        ]);
    }

    public function sendDisbursementCode(string $phone, string $code, int $ttlMinutes): SmsSendResult
    {
        return $this->logSend('disbursement_code', $phone, [
            'code' => $code,
            'ttl_minutes' => $ttlMinutes,
        ]);
    }

    private function logSend(string $type, string $phone, array $payload): SmsSendResult
    {
        $messageId = sprintf('%s_%s', $type, uniqid());

        $this->logger->info('[SMS][LogDriver] sending sms', [
            'type' => $type,
            'phone' => $phone,
            'payload' => $payload,
            'message_id' => $messageId,
        ]);

        return SmsSendResult::success($messageId, [
            'driver' => 'log',
            'type' => $type,
        ]);
    }
}
